/*
 * Copyright (c) 2006-2024, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-08-11     RT-Thread    first version
 */
#include <rtthread.h>
#include <rtdevice.h>
#include "debug.h"

#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

#define SOUND_DEBUF_ENABLE  0

#define SOUND_DEVICE_NAME    "sound0"    /* Audio 设备名称 */
static rt_device_t snd_dev;              /* Audio 设备句柄 */

static int rt_audio_stream_open(void)
{
    rt_err_t ret;
    snd_dev = rt_device_find(SOUND_DEVICE_NAME);
    if (snd_dev != NULL)
    {
        ret = rt_device_open(snd_dev, RT_DEVICE_OFLAG_RDWR);
        if (ret == RT_EOK)
        {
            rt_device_control(snd_dev, AUDIO_CTL_START, NULL);
        }
    }
    return 0;
}

static int rt_audio_stream_close(void)
{
    snd_dev = rt_device_find(SOUND_DEVICE_NAME);
    if (snd_dev != NULL)
    {
        rt_device_control(snd_dev, AUDIO_CTL_STOP, NULL);
    }
    return 0;
}

static void rt_audio_debug(void)
{
    uint16_t buffer[640];
    rt_audio_stream_open();

    while(1)
    {
        rt_device_read(snd_dev, 0, buffer, 1280);
        rt_device_write(snd_dev, 0, buffer, 1280);
    }
}

int main(void)
{
    LOG_I("Hello RT-Thread!");

    DEBUG_PIN_INIT();

#if SOUND_DEBUF_ENABLE == 1
    rt_audio_debug();
#endif

    return RT_EOK;
}



